package com.som.service.excel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.som.beanconvert.ExcelDataParseOutputInf;
import com.som.console.ErrorCode;
import com.som.console.ExcelProcException;

/**
 * 读取excel模板，然后进行写入操作
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public abstract class ExcelReadWriteProcess<T>
{
	/**
	 * 通过过此抽换方法调用实现
	 * 
	 * @return
	 */
	protected abstract ExcelDataParseOutputInf<T> getToExcelInstall();

	/**
	 * 读取excel模板，将list数据写入
	 * 
	 * @throws ExcelProcException
	 */
	public void readWriteExcelByList(String path, int sheetId, boolean isjumpFist, List<T> writeList) throws ExcelProcException
	{
		POIFSFileSystem fs = null;
		FileOutputStream output = null;
		HSSFWorkbook wb = null;
		try
		{
			fs = new POIFSFileSystem(new FileInputStream(path));

			// 得到Excel工作簿对象
			wb = new HSSFWorkbook(fs);
			// 得到Excel工作表对象
			HSSFSheet sheet = wb.getSheetAt(sheetId);

			if (null != writeList && !writeList.isEmpty())
			{
				ExcelDataParseOutputInf<T> writeExcel = getToExcelInstall();

				for (int i = 0; i < writeList.size(); i++)
				{

					HSSFRow row = null;
					if (isjumpFist)
					{
						row = sheet.createRow(i + 1);
					} else
					{
						row = sheet.createRow(i);
					}

					writeExcel.parseExcelRow(writeList.get(i), row);
				}
			}

			output = new FileOutputStream(path);

			wb.write(output);

			output.flush();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_NOTFOUNT.getMsg());
		} catch (IOException e)
		{
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_FILE_IS_ERROR.getMsg());
		} finally
		{
			IOUtils.closeQuietly(output);
			IOUtils.closeQuietly(wb);
		}
	}

	/**
	 * 读取excel模板，将map数据写入
	 * 
	 * @throws ExcelProcException
	 */
	public void readWriteExcelByMap(String path, int sheetId, boolean isjumpFist, Map<String, T> writeMap) throws ExcelProcException
	{
		POIFSFileSystem fs = null;
		FileOutputStream output = null;
		HSSFWorkbook wb = null;
		try
		{
			fs = new POIFSFileSystem(new FileInputStream(path));

			// 得到Excel工作簿对象
			wb = new HSSFWorkbook(fs);
			// 得到Excel工作表对象
			HSSFSheet sheet = wb.getSheetAt(sheetId);

			if (null != writeMap && !writeMap.isEmpty())
			{
				ExcelDataParseOutputInf<T> writeExcel = getToExcelInstall();

				Iterator<Entry<String, T>> writeIter = writeMap.entrySet().iterator();

				int i = 0;

				while (writeIter.hasNext())
				{
					// 跳过首行
					if (isjumpFist)
					{
						isjumpFist = false;
						i += 1;
						continue;
					}

					HSSFRow row = sheet.createRow(i);

					writeExcel.parseExcelRow(writeIter.next().getValue(), row);
					i += 1;
				}
			}

			output = new FileOutputStream(path);

			wb.write(output);

			output.flush();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_NOTFOUNT.getMsg());
		} catch (IOException e)
		{
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_FILE_IS_ERROR.getMsg());
		} finally
		{
			IOUtils.closeQuietly(output);
			IOUtils.closeQuietly(wb);
		}
	}
}
